package com.yingxin.yxerp.module.sys.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yingxin.yx.framework.commons.constant.CommonConst;
import com.yingxin.yx.framework.commons.exception.BizException;
import com.yingxin.yx.framework.commons.id.IdHelper;
import com.yingxin.yx.framework.user.core.IUserSupport;
import com.yingxin.yxerp.module.sys.entity.OtherSet;
import com.yingxin.yxerp.module.sys.service.IOtherSetService;
import com.yingxin.yxerp.module.sys.service.ITradeManagementInvoiceNameSettingService;
import com.yingxin.yxerp.module.sys.vo.req.OtherSetCreateVo;
import com.yingxin.yxerp.module.sys.vo.req.OtherSetQueryVo;
import com.yingxin.yxerp.module.sys.vo.resp.OtherSetRespVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 贸易管理开票名称设置 接口 服务实现
 * @author yuebao
 * @since 2023-12-28
 */

@Service
public class TradeManagementInvoiceNameSettingServiceImpl
        implements ITradeManagementInvoiceNameSettingService {

    @Autowired
    private IOtherSetService otherSetService;

    @Autowired
    private IUserSupport userSupport;

    /**
     * 数据类型
     * @return
     */
    @Override
    public String getSettingType(){
        return "TMINSS";
    }

    /**
     * 查询全部
     */
    @Override
    public List<OtherSetRespVo> listVo(OtherSetQueryVo queryVo) {
        queryVo.setOtherType( getSettingType() );
        return otherSetService.listVo( queryVo );
    }

    /**
     * 根据 id 查询
     */
    @Override
    public OtherSetRespVo getVo(String id) {
        return otherSetService.getVo( id );
    }

    /**
     * 删除
     */
    @Override
    public void delete(String id) {
        otherSetService.delete( id );
    }

    /**
     * 创建
     */
    @Override
    public void create(OtherSetCreateVo createVo) {
        // 验证
        checkName( null, createVo.getName(), CommonConst.FALSE );

        OtherSet otherSet = new OtherSet();
        if( StringUtils.isEmpty( createVo.getId() ) ){
            otherSet.setId( IdHelper.snowflakeId() );
        }
        otherSet.setName( createVo.getName() );
        // otherSet.setValue( null );
        otherSet.setType( getSettingType() );

        otherSetService.create( otherSet );
    }

    /**
     * 修改
     */
    @Override
    public void update(OtherSetCreateVo createVo) {
        // 验证
        checkName( createVo.getId(), createVo.getName(), CommonConst.TRUE );

        OtherSet otherSet = otherSetService.getById( createVo.getId() );
        if( ObjectUtil.isEmpty( otherSet ) ){
            throw new BizException("未找到对象！");
        }

        otherSet.setName( createVo.getName() );
        otherSet.setCreatorId( userSupport.getUserId() );
        otherSet.setCreateTime( new Date() );

        otherSetService.updateById( otherSet );
    }

    /**
     * 检测是否重名
     * @param name
     * @param settingType 0 = 新增， 1 = 修改
     */
    private void checkName( String id, String name, String settingType ){
        if( StringUtils.isEmpty( name ) ){
            throw new BizException("名称不能为空！");
        }

        // 新增
        if( CommonConst.FALSE.equals( settingType ) ){
            QueryWrapper<OtherSet> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq( OtherSet::getType, getSettingType() );
            queryWrapper.lambda().eq( OtherSet::getDelFlag, CommonConst.FALSE );
            queryWrapper.lambda().eq( OtherSet::getName, name );
            List<OtherSet> list = otherSetService.mapper().selectList( queryWrapper );
            if( CollectionUtil.isNotEmpty( list ) ){
                throw new BizException( String.format( "开票名称：[ %s ] 重复！", name ) );
            }
        }
        // 修改
        if( CommonConst.TRUE.equals( settingType ) ){
            if( StringUtils.isEmpty( id ) ){
                throw new BizException("数据异常，ID 为空！");
            }

            QueryWrapper<OtherSet> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq( OtherSet::getType, getSettingType() );
            queryWrapper.lambda().eq( OtherSet::getDelFlag, CommonConst.FALSE );
            queryWrapper.lambda().eq( OtherSet::getName, name );
            queryWrapper.lambda().ne( OtherSet::getId, id );
            List<OtherSet> list = otherSetService.mapper().selectList( queryWrapper );
            if( CollectionUtil.isNotEmpty( list ) ){
                throw new BizException( String.format( "开票名称：[ %s ] 重复！", name ) );
            }
        }
    }

}
